package com.youdoneed.common.controller;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.common.service.AliOssSTS;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author Ewing
 * @since 2017-04-21
 **/
@RestController
@RequestMapping("/aliOss")
@Api(value = "阿里OSS服务", description = "阿里OSS服务")
public class AliOssController {

    /**
     * 获取服务器时间。
     */
    @ApiOperation("获取服务器时间")
    @RequestMapping(value = "/nowDate", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Date> nowDate() {
        return new ResultMessage<>(new Date());
    }

    /**
     * 获取上传凭证（STS凭证）。
     */
    @ApiOperation("获取上传凭证（STS凭证）")
    @RequestMapping(value = "/getSTS", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<AliOssSTS.STSResult> getSTS(
            @ApiParam("用户ID，若无则使用Android、IOS或Web代替") @RequestParam String roleName) {
        if (!StringUtils.hasText(roleName)) {
            roleName = "Default";
        }
        return new ResultMessage<>(AliOssSTS.assumeSTSToken(roleName));
    }

}
